﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Transport;

namespace Mimoza.DBLayer.Transport.Server
{
    public class ConnectionList : IDisposable
    {
        public ConnectionList()
        {

        }

        public void Add(Connection conn)
        {
            lock (m_connections)
            {
                m_connections.Add(conn);
            }
        }

        public void SendRequest(TransferObject request)
        {
            lock (m_connections)
            {
                for( int i = 0; i < m_connections.Count; i++)
                {
                    try
                    {
                        m_connections[i].SendRequest(request);
                    }
                    catch (Exception )
                    {
                        Mimoza.Common.Logger.Log.Warn("CollectionList connection is closed. Remove from list.");

                        m_connections[i].Close();
                        m_connections.Remove(m_connections[i]);
                        --i;
                    }
                }
            }
        }

        public void Dispose()
        {
            lock (m_connections)
            {
                m_connections.ForEach(x => x.Close());
                m_connections.Clear();
            }
        }

        List<Connection> m_connections = new List<Connection>();
    }
}
